<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';
	
	require_once $codeConfig->C_STR_USER_MANAGER_PATH . "/Classes/cTABLE_Users.php";
	require_once dirname(__DIR__) . "/User_Manager/cUserValidation.php";

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 07 NOV 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_GroupPermissions extends cTable
	{

		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_UserID = null;
		public $m_int_COL_GroupID = null;
		public $m_int_COL_Admin = null;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "group_permissions";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the permission.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_UserID = new cColVarChar("userId");
			$obj_COL_UserID->set_Length(36);
			$obj_COL_UserID->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_UserID->set_Comment("This column holds the User ID of the User.");
			$obj_COL_UserID->set_Label("User ID");

			$obj_COL_GroupID = new cColVarChar("groupId");
			$obj_COL_GroupID->set_Length(36);
			$obj_COL_GroupID->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_GroupID->set_Comment("This column holds the Group ID.");
			$obj_COL_GroupID->set_Label("Group ID");

			$obj_COL_Admin = new cColTinyInt("admin");
			$obj_COL_Admin->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Admin->set_Comment("This column holds Weather the user is an admin for the group 1=admin 0= non admin.");
			$obj_COL_Admin->set_Label("Administrator");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//$this->m_obj_Connection->activate_Logging();
			//$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_Admin = $this->add_Column($obj_COL_Admin);
			$this->m_int_COL_GroupID = $this->add_Column($obj_COL_GroupID);
			$this->m_int_COL_UserID = $this->add_Column($obj_COL_UserID);
		}

		/**
		 * Deletes the user based on
		 * @param string $str_UserID ID of the User (GUID)
		 * @return mixed passed from Delete function
		 */
		private function delete_User($str_UserID, $str_GroupID)
		{
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_UserID, $str_UserID);
			$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_GroupID);
			if ($this->do_Delete() < 0)
			{
				return false;
			}

			return true;
		}

		/**
		 * Deletes a user using the user ID
		 * @param string $str_UserID ID of the User (GUID)
		 * @return mixed passed from Delete function
		 */
		public function delete_UserID($str_UserID, $str_GroupID)
		{
			return $this->delete_User($str_UserID, $str_GroupID);
		}

		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			$this->set_id($this->create_GUID());

			return parent::insert_Data2DB();
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}
		}

		public function insert_DefaultData()
		{
			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Returns if the id combo is existing
		 * @param string $str_group_ID ID of the Group to check (GUID)
		 * @param string $str_usr_ID ID of the User to check (GUID)
		 * @return boolean true if exists, false if not
		 */
		public function add_by_sid($str_ID, $str_group_ID, $str_admin)
		{
			$this->m_obj_TABLE_Users = new \cTable_Users();
			if ($this->m_obj_TABLE_Users->validate_UserName($str_ID))
			{
				$str_usr_ID = $this->m_obj_TABLE_Users->get_IDFromUserName($str_ID);
				if ($this->add_by_guid($str_usr_ID, $str_group_ID, $str_admin))
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Returns if the id combo is existing
		 * @param string $str_group_ID ID of the Group to check (GUID)
		 * @param string $str_usr_ID ID of the User to check (GUID)
		 * @return boolean true if exists, false if not
		 */
		public function add_by_guid($str_usr_ID, $str_group_ID, $str_admin)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			//print($str_usr_ID."::".$str_group_ID);
			if ((!empty($str_group_ID) && !empty($str_usr_ID)) && (preg_match(self::c_REG_GUID_VALID, $str_usr_ID) && preg_match(self::c_REG_GUID_VALID, $str_group_ID)))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_group_ID);
				$this->add_ColumnQuery($this->m_int_COL_UserID, $str_usr_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						$this->set_admin($str_admin);
						$this->save_Data2DB();
						return true;
					}

				}
				else
				{
					$str_ID = $this->create_GUID();
					$this->set_id($str_ID);
					$this->set_admin($str_admin);
					$this->set_groupId($str_group_ID);
					$this->set_userId($str_usr_ID);
					$this->save_Data2DB();
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the id to check (int)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if ($this->validate_ID($str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}

		/**
		 *
		 */
		public function is_admin($str_userID, $str_groupID)
		{
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_UserID, $str_userID);
			$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_groupID);
			if ($this->do_Search())
			{
				$obj_RS = $this->get_ResultSet();
				if ($obj_RS[0])
				{
					$this->load_DataFromArray($obj_RS[0]);
					if ($this->get_admin() > 0)
					{
						return true;
					}
				}
			}
			return false;
		}

		public function is_Member($str_userID, $str_groupID)
		{
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_UserID, $str_userID);
			$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_groupID);
			if ($this->do_Search() > 0)
			{
				return true;
			}
			return false;
		}

		/**
		 *
		 */
		public function admin_status_change($str_userID, $str_groupID)
		{
			$obj_UserValidation = new cUserValidation();
			if ($str_userID != $obj_UserValidation->get_UserID())
			{
				$obj_GroupPermissions = new cTABLE_GroupPermissions();
				$obj_GroupPermissions->clear_Query();
				$obj_GroupPermissions->add_ColumnQuery($this->m_int_COL_UserID, $str_userID);
				$obj_GroupPermissions->add_ColumnQuery($this->m_int_COL_GroupID, $str_groupID);
				if ($obj_GroupPermissions->do_Search())
				{
					$obj_RS = $obj_GroupPermissions->get_ResultSet();
					if ($obj_RS[0])
					{
						$obj_GroupPermissions->load_DataFromArray($obj_RS[0]);
						if ($obj_GroupPermissions->get_admin() > 0)
						{

							$obj_GroupPermissions->set_admin('0');
						}
						else
						{
							$obj_GroupPermissions->set_admin('1');
						}
						$obj_GroupPermissions->save_Data2DB();
						return true;
					}
				}
			}
			return false;
		}

	}
?>
